package edu.msse.signup.service;


import java.util.Iterator;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

import org.springframework.orm.jpa.support.JpaDaoSupport;

import edu.msse.signup.domain.Event;
import edu.msse.signup.domain.Position;
import edu.msse.signup.domain.TimeSlot;
import edu.msse.signup.domain.Users;

public class PositionService extends JpaDaoSupport{
	
	
	
	static public Position createPosition(String name, String description, int numberOfVolunteer, Event event)
	{
		EntityManagerFactory emf =
	        Persistence.createEntityManagerFactory("pu");
		EntityManager jpa = emf.createEntityManager();
		
        EntityTransaction tx = jpa.getTransaction();
        try {
            tx.begin();
            if(event !=null)
        	{
            	event=jpa.merge(event);
        	}
            Position p=new Position(name,description,numberOfVolunteer,event);
            jpa.persist(p);
            p=jpa.merge(p);
            tx.commit();
            return p;
        } catch (Exception x) {
        	x.printStackTrace();
        	tx.rollback();
        	return null;
        }
    }
	
	static public Position addTimeSlot(Position p, TimeSlot t)
	{
		EntityManagerFactory emf =
	        Persistence.createEntityManagerFactory("pu");
		EntityManager jpa = emf.createEntityManager();
		
		
        EntityTransaction tx = jpa.getTransaction();
        try {
            tx.begin();
            t=jpa.merge(t);
            p=jpa.merge(p);
            p.addTimeSlot(t);
            jpa.persist(p);
            tx.commit();
            
        } catch (Exception x) {
        	x.printStackTrace();
        	tx.rollback();
        	return null;
            
        }
        
        return p;
	}
	
	static public Position addEvent(Position p, Event e)
	{
		EntityManagerFactory emf =
	        Persistence.createEntityManagerFactory("pu");
		EntityManager jpa = emf.createEntityManager();
		
		
        EntityTransaction tx = jpa.getTransaction();
        try {
            tx.begin();
            e=jpa.merge(e);
            p.addEvent(e);
            p=jpa.merge(p);
            jpa.persist(p);
            tx.commit();
            
        } catch (Exception x) {
        	x.printStackTrace();
        	tx.rollback();
        	return null;
            
        }
        
        return p;
	}
	
	
	public Users addUser(Position position, Users user)
	{
		EntityManagerFactory emf =
	        Persistence.createEntityManagerFactory("pu");
		EntityManager jpa = emf.createEntityManager();
		
        EntityTransaction tx = jpa.getTransaction();
        try {
            tx.begin();
            position = jpa.merge(position);
            if(position.getUsers().size() < position.getNumberOfVolunteers()){
                user=jpa.merge(user);
                user.addPosition(position);
                position = jpa.merge(position);
                position.addUser(user);
            	user=jpa.merge(user);
                jpa.persist(position);
                jpa.persist(user);
            }
            else 
            	throw new Exception("Position Already Filled");
            
            tx.commit();
        	return user;
        } catch (Exception x) {
        	x.printStackTrace();
        	tx.rollback();
        	return null;
        }
    }
	
	public static Position update(Position position)
	{
		EntityManagerFactory emf =
	        Persistence.createEntityManagerFactory("pu");
		EntityManager jpa = emf.createEntityManager();
		
        EntityTransaction tx = jpa.getTransaction();
        try {
            tx.begin();
            position=jpa.merge(position);
            jpa.persist(position);
            tx.commit();
            
        } catch (Exception x) {
        	x.printStackTrace();
        	tx.rollback();
        	return null;
            
        }
		return position;
	}
	
	public Position getPosition(int id){
		return getJpaTemplate().find(Position.class, id);
	}
	
	@SuppressWarnings("unchecked")
	public List<TimeSlot> getPositionTimeslots(Position position) {
	
		 return getJpaTemplate().find(
	                "from TimeSlot t where t.position = ?", position);
	}
	
	public List getUsers(Position position){
		return getJpaTemplate().find("from Users u where ? in elements(u.positions)", position);
	}
	
	public static Boolean delete(Position position)
	{
		EntityManagerFactory emf =
	        Persistence.createEntityManagerFactory("pu");
		EntityManager jpa = emf.createEntityManager();
		
        EntityTransaction tx = jpa.getTransaction();
        try {
            tx.begin();
            position=jpa.merge(position);
            position.getEvent().getPositions().remove(position);
            List users = position.getUsers();
            Iterator i = users.iterator();
            while(i.hasNext()){
            	Users user = (Users)i.next();
            	user = jpa.merge(user);
            	user.getPositions().remove(position);
            	user = jpa.merge(user);
            }
            position.getUsers().removeAll(users);
            position = jpa.merge(position);
            jpa.remove(position);
            tx.commit();
            return true;
            
        } catch (Exception x) {
        	x.printStackTrace();
        	tx.rollback();
        	return false;
            
        }
		
	}
}
